package com.qersoft.service;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qersoft.dao.PaperMapper;
import com.qersoft.entity.Paper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PaperServiceImpl extends ServiceImpl<PaperMapper, Paper> implements PaperService {

    @Autowired
    private PaperMapper paperMapper;

    @Override
    public boolean savePaper(Paper paper) {
        try {
            paperMapper.insert(paper);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override//根据id查询试卷
    public Paper getPaperById(Long id) {
        return paperMapper.selectById(id);
    }

    @Override//根据标题模糊查询试卷
    public List<Paper> getPapersByTitle(String title) {
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        return paperMapper.selectList(queryWrapper);
    }

    @Override//当试卷标题为null时，获取所有试卷
    public List<Paper> getAllPapers() {
        QueryWrapper<Paper> queryWrapper = new QueryWrapper<>();
        // 这里可以根据后续可能的扩展需求，添加一些通用的查询条件限制，比如只查询状态为已发布的试卷等
        // 目前无额外筛选条件，所以直接调用selectList方法并传入queryWrapper
        return paperMapper.selectList(queryWrapper);
    }

    @Override//分页查询试卷
    public IPage<Paper> getPapersByPage(int currentPage, int pageSize) {
        Page<Paper> page = new Page<>(currentPage, pageSize);
        return paperMapper.selectPage(page, null);
    }
    @Override
    public boolean deletePaperById(Long id) {
        try {
            int rows = baseMapper.deleteById(id); // 获取受影响的行数
            return rows > 0; // 如果 rows > 0，表示删除成功；否则删除失败
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    @Override
    public boolean updatePaper(Paper paper) {
        try {
            UpdateWrapper<Paper> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", paper.getId()); // 根据id更新

            // 动态更新字段
            if (paper.getTitle() != null) {
                updateWrapper.set("title", paper.getTitle());
            }
            if (paper.getTotalScore() != null) {
                updateWrapper.set("total_score", paper.getTotalScore());
            }
            if (paper.getTimeLimit() != null) {
                updateWrapper.set("time_limit", paper.getTimeLimit());
            }
            if (paper.getStatus() != null) {
                updateWrapper.set("status", paper.getStatus());
            }
            if (paper.getDescription() != null) {
                updateWrapper.set("description", paper.getDescription());
            }

            int rows = paperMapper.update(null, updateWrapper); // 执行更新
            return rows > 0; // 返回是否更新成功
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}